import router from './index'
import store from '@/store'

import NProgress from "nprogress";
import "nprogress/nprogress.css";

// 进度条
NProgress.configure({
    // 动画方式
    easing: "ease",
    // 递增进度条的速度
    speed: 500,
    // 是否显示加载ico
    showSpinner: false,
    // 自动递增间隔
    trickleSpeed: 200,
    // 初始化时的最小百分比
    minimum: 0.3,
});

// 4.全局前置守卫
router.beforeEach(async (to, from, next) => {

    NProgress.start();
    const token = store.state.UserToken;
    if (!token) {

        // 未登录，是否需要登录
        if (to.matched.length > 0 && !to.matched.some(record => record.meta.requiresAuth)) {

            next()
            NProgress.done();
        } else {

            next({
                path: "/login"
            })

            NProgress.done();
        }
    } else {
        // 已经登录 路由的访问权限 permisiionList

        if (!store.state.permission.permissionList.length) {
            // console.log(store.state.permission.permissionList)

            await store.dispatch('permission/FETCH_PERMISSION',)



            next({ path: to.path, replace: true })
            NProgress.done();



        } else {

            if (to.path != '/login') {

                next()
                NProgress.done();
            } else {

                next(from.fullPath)
                NProgress.done();
            }
        }
    }


})